package org.liaohailong.helloworld;

import org.junit.Test;
import org.liaohailong.helloworld.array.ArraySolution;
import org.liaohailong.helloworld.array.StringSolution;

import java.util.Arrays;

/**
 * Author: liaohailong
 * Time: 2021/5/12 09:46
 * Describe:
 */
public class Exam2 implements AUtil.ILog {

    @Test
    public void searchInsert() {
        ArraySolution as = new ArraySolution();
        int[] nums = new int[]{1, 3, 5, 6};
        int target = 5;
        int i = as.searchInsert(nums, target);
        log("i = " + i);
    }

    @Test
    public void merge() {
        ArraySolution as = new ArraySolution();
        int[][] arr = new int[2][];
        arr[0] = new int[]{1, 4};
        arr[1] = new int[]{1, 4};
        int[][] merge = as.merge(arr);
        for (int[] ints : merge) {
            log(Arrays.toString(ints));
        }
    }

    @Test
    public void rotate() {
        ArraySolution as = new ArraySolution();
//        int[][] arr = new int[3][3];
//        arr[0] = new int[]{1, 2, 3};
//        arr[1] = new int[]{4, 5, 6};
//        arr[2] = new int[]{7, 8, 9};

        int[][] arr = new int[4][4];
        arr[0] = new int[]{1, 2, 3, 4};
        arr[1] = new int[]{5, 6, 7, 8};
        arr[2] = new int[]{9, 10, 11, 12};
        arr[3] = new int[]{13, 14, 15, 16};
        as.rotate(arr);
        for (int[] ints : arr) {
            log(Arrays.toString(ints));
        }
    }

    @Test
    public void setZeroes() {
        AUtil.setLog(this);

        ArraySolution as = new ArraySolution();
//        int[][] arr = new int[5][4];
//        arr[0] = new int[]{0, 0, 0, 5};
//        arr[1] = new int[]{4, 3, 1, 4};
//        arr[2] = new int[]{0, 1, 1, 4};
//        arr[3] = new int[]{1, 2, 1, 3};
//        arr[4] = new int[]{0, 0, 1, 1};

        int[][] arr = new int[3][3];
        arr[0] = new int[]{1, 1, 1};
        arr[1] = new int[]{1, 0, 1};
        arr[2] = new int[]{1, 1, 1};

        AUtil.printCostInMs(() -> as.setZeroes(arr), "setZeroes");
        for (int[] ints : arr) {
            log(Arrays.toString(ints));
        }
    }

    @Test
    public void findDiagonalOrder() {
        AUtil.setLog(this);
        ArraySolution as = new ArraySolution();

        int[][] arr = new int[3][3];
        arr[0] = new int[]{1, 2, 3};
        arr[1] = new int[]{4, 5, 6};
        arr[2] = new int[]{7, 8, 9};

        int[] result = as.findDiagonalOrder(arr);
        log(Arrays.toString(result));
    }

    @Test
    public void string() {
        // initialize
        String s1 = "Hello World";
        System.out.println("s1 is \"" + s1 + "\"");
        String s2 = s1;
        System.out.println("s2 is another reference to s1.");
        String s3 = new String(s1);
        System.out.println("s3 is a copy of s1.");
        // compare using '=='
        System.out.println("Compared by '==':");
        // true since string is immutable and s1 is binded to "Hello World"
        System.out.println("s1 and \"Hello World\": " + (s1 == "Hello World"));
        // true since s1 and s2 is the reference of the same object
        System.out.println("s1 and s2: " + (s1 == s2));
        // false since s3 is refered to another new object
        System.out.println("s1 and s3: " + (s1 == s3));

        // compare using 'equals'
        System.out.println("Compared by 'equals':");
        System.out.println("s1 and \"Hello World\": " + s1.equals("Hello World"));
        System.out.println("s1 and s2: " + s1.equals(s2));
        System.out.println("s1 and s3: " + s1.equals(s3));
        // compare using 'compareTo'
        System.out.println("Compared by 'compareTo':");
        System.out.println("s1 and \"Hello World\": " + (s1.compareTo("Hello World") == 0));
        System.out.println("s1 and s2: " + (s1.compareTo(s2) == 0));
        System.out.println("s1 and s3: " + (s1.compareTo(s3) == 0));
    }


    @Test
    public void longestCommonPrefix() {
        StringSolution ss = new StringSolution();
//        String[] strs = {"flower", "flow", "flight"};
        String[] strs = {"dog", "racecar", "car"};
        String s = ss.longestCommonPrefix(strs);
        log(s);
    }

    @Test
    public void longestPalindrome() {
        AUtil.setLog(this);
        StringSolution ss = new StringSolution();
//        String str = "babad";
//        String str = "cbbd";
//        String str = "abb";
//        String str = "eabcb";
//        String str = "a";
//        String str = "aacabdkacaa";
//        String str = "iopsajhffgvrnyitusobwcxgwlwniqchfnssqttdrnqqcsrigjsxkzcmuoiyxzerakhmexuyeuhjfobrmkoqdljrlojjjysfdslyvckxhuleagmxnzvikfitmkfhevfesnwltekstsueefbrddxrmxokpaxsenwlgytdaexgfwtneurhxvjvpsliepgvspdchmhggybwupiqaqlhjjrildjuewkdxbcpsbjtsevkppvgilrlspejqvzpfeorjmrbdppovvpzxcytscycgwsbnmspihzldjdgilnrlmhaswqaqbecmaocesnpqaotamwofyyfsbmxidowusogmylhlhxftnrmhtnnljjhhcfvywsqimqxqobfsageysonuoagmmviozeouutsiecitrmkypwknorjjiaasxfhsftypspwhvqovmwkjuehujofiabznpipidhfxpoustquzyfurkcgmioxacleqdxgrxbldcuxzgbcazgfismcgmgtjuwchymkzoiqhzaqrtiykdkydgvuaqkllbsactntexcybbjaxlfhyvbxieelstduqzfkoceqzgncvexklahxjnvtyqcjtbfanzgpdmucjlqpiolklmjxnscjcyiybdkgitxnuvtmoypcdldrvalxcxalpwumfx";

        AUtil.printCostInMs(() -> {
            for (int i = 0; i < 180; i++) {
//                String str = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
                String str = "iopsajhffgvrnyitusobwcxgwlwniqchfnssqttdrnqqcsrigjsxkzcmuoiyxzerakhmexuyeuhjfobrmkoqdljrlojjjysfdslyvckxhuleagmxnzvikfitmkfhevfesnwltekstsueefbrddxrmxokpaxsenwlgytdaexgfwtneurhxvjvpsliepgvspdchmhggybwupiqaqlhjjrildjuewkdxbcpsbjtsevkppvgilrlspejqvzpfeorjmrbdppovvpzxcytscycgwsbnmspihzldjdgilnrlmhaswqaqbecmaocesnpqaotamwofyyfsbmxidowusogmylhlhxftnrmhtnnljjhhcfvywsqimqxqobfsageysonuoagmmviozeouutsiecitrmkypwknorjjiaasxfhsftypspwhvqovmwkjuehujofiabznpipidhfxpoustquzyfurkcgmioxacleqdxgrxbldcuxzgbcazgfismcgmgtjuwchymkzoiqhzaqrtiykdkydgvuaqkllbsactntexcybbjaxlfhyvbxieelstduqzfkoceqzgncvexklahxjnvtyqcjtbfanzgpdmucjlqpiolklmjxnscjcyiybdkgitxnuvtmoypcdldrvalxcxalpwumfx";
//                String str = "eeeee";
                String s = ss.longestPalindrome(str);
//                log(s);
            }
        }, "longestPalindrome");
    }

    @Test
    public void reverseWords() {
        AUtil.setLog(this);
        StringSolution ss = new StringSolution();
        String str = "Let's take LeetCode contest";
        String s = ss.reverseWords(str);
        log(s);
    }

    @Test
    public void indexOf() {
        AUtil.setLog(this);
        StringSolution ss = new StringSolution();
//        int i = ss.indexOf("abccbaba", "ccba");
//        String p = "ACTGPACY";
//        String s = "ACTGPACTGKACTGPACY";

//        String p = "ccba";
//        String s = "abccbaba";

        String p = "aaaa";
        String s = "aaa";
        int i = ss.match(p, s);
        log("" + i);
    }

    @Test
    public void removeElement() {
        AUtil.setLog(this);
        StringSolution ss = new StringSolution();
//        int[] nums = new int[]{3, 2, 2, 3};
//        int target = 3;
        int[] nums = new int[]{0, 1, 2, 2, 3, 0, 4, 2};
        int target = 2;
        int i = ss.removeElement(nums, target);
        log(i + ": " + Arrays.toString(nums));
    }

    @Test
    public void minSubArrayLen() {
        AUtil.setLog(this);
        StringSolution ss = new StringSolution();
        int target = 11;
        int[] nums = new int[]{1, 2, 3, 4, 5};
        int i = ss.minSubArrayLen(target, nums);
        log(i + "");
    }

    public void log(String msg) {
        log("Exam1", msg);
    }

    @Override
    public void log(String tag, String msg) {
        System.out.println(tag + ": " + msg);
    }
}
